package net.lab1024.smartadmin.module.business.driveshaft.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lab1024.smartadmin.common.constant.ResponseCodeConst;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.business.driveshaft.domain.dto.DriveshaftAddDTO;
import net.lab1024.smartadmin.module.business.driveshaft.domain.dto.DriveshaftQueryDTO;
import net.lab1024.smartadmin.module.business.driveshaft.domain.dto.DriveshaftUpdateDTO;
import net.lab1024.smartadmin.module.business.driveshaft.domain.entity.DriveshaftEntity;
import net.lab1024.smartadmin.module.business.driveshaft.domain.vo.DriveshaftExcelVO;
import net.lab1024.smartadmin.module.business.driveshaft.domain.vo.DriveshaftVO;
import net.lab1024.smartadmin.module.business.driveshaft.service.DriveshaftService;
import net.lab1024.smartadmin.util.SmartRequestTokenUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * [ ������ ]
 *
 * @author �Ζ�
 * @version 1.0
 * @company ��������
 * @copyright (c)  ��������Inc. All rights reserved.
 * @date 2021-11-19 11:18:27
 * @since JDK1.8
 */
@RestController
@Api(tags = {"������"})
public class DriveshaftController extends BaseController {

    @Autowired
    private DriveshaftService driveshaftService;

    @ApiOperation(value = "����id��ȡ����",notes = "@author �Ζ�")
    @GetMapping("/driveshaft/getById/{id}")
    public ResponseDTO<DriveshaftEntity> getById(@PathVariable Long id) {
        return ResponseDTO.succData(driveshaftService.getById(id));
    }

    @ApiOperation(value = "��ҳ��ѯ������",notes = "@author �Ζ�")
    @PostMapping("/driveshaft/page/query")
    public ResponseDTO<PageResultDTO<DriveshaftVO>> queryByPage(@RequestBody DriveshaftQueryDTO queryDTO) {
        if(queryDTO.getCreateUser()!=null){
            queryDTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        }queryDTO.setIsIndexShow(1);
        queryDTO.setMyUserId(SmartRequestTokenUtil.getRequestUserId());
        return driveshaftService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "��Ӵ�����",notes = "@author �Ζ�")
    @PostMapping("/driveshaft/add")
    public ResponseDTO<String> add(@RequestBody @Validated DriveshaftAddDTO addTO){
        addTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        addTO.setCreateTime(new Date());
        if(driveshaftService.select(addTO.getSn(),null)){
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_SN_REPEAT);
        }
        return driveshaftService.add(addTO);
    }

    @ApiOperation(value="�޸Ĵ�����",notes = "@author �Ζ�")
    @PostMapping("/driveshaft/update")
    public ResponseDTO<String> update(@RequestBody @Validated DriveshaftUpdateDTO updateDTO){
        if(driveshaftService.select(updateDTO.getSn(),updateDTO.getId())){
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_SN_REPEAT);
        }
        return driveshaftService.update(updateDTO);
    }

    @ApiOperation(value="����ɾ��������",notes = "@author �Ζ�")
    @PostMapping("/driveshaft/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return driveshaftService.deleteByIds(idList);
    }

    @ApiOperation(value = "��������", notes = "@author �Ζ�")
    @PostMapping("/driveshaft/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //��ѯ����
        List<DriveshaftExcelVO> driveshaftList = driveshaftService.queryBatchExportData(idList);
        //��������
        ExportParams ex = new ExportParams("������", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, DriveshaftExcelVO.class, driveshaftList);
        downloadExcel("������", workbook, response);
    }

    @ApiOperation(value = "����ȫ��", notes = "@author �Ζ�")
    @PostMapping("/driveshaft/export/all")
    public void exportAll(@RequestBody @Validated DriveshaftQueryDTO queryDTO, HttpServletResponse response) {
        //��ѯ����
        List<DriveshaftExcelVO> driveshaftList = driveshaftService.queryAllExportData(queryDTO);
        //��������
        ExportParams ex = new ExportParams("������", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, DriveshaftExcelVO.class, driveshaftList);
        downloadExcel("������", workbook, response);
    }

}
